package cn.dnuifood_cjy.dnuifood_16110100529.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.dnuifood_cjy.dnuifood_16110100529.JavaBean.ResultBean;
import cn.dnuifood_cjy.dnuifood_16110100529.R;
import cn.dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import cn.dnuifood_cjy.dnuifood_16110100529.model.UserModel;


public class RegActivity extends BaseActivity implements OnRetrofitListener<ResultBean> {

    private Intent intent;
    private Button register;
    private EditText et_reg_user,et_reg_password,et_reg_repassword,et_reg_telephone,et_reg_adress,et_reg_comment;
    private UserModel userModel;
    private Button reg_return;
    @Override
    void initViews() {
        setLayout(R.layout.activity_reg);
        register=(Button)findViewById(R.id.register);
        et_reg_user=(EditText)findViewById(R.id.et_reg_user);
        et_reg_password=(EditText)findViewById(R.id.et_reg_password);
        et_reg_repassword=(EditText)findViewById(R.id.et_reg_repassword);
        et_reg_telephone=(EditText)findViewById(R.id.et_reg_telephone);
        et_reg_adress=(EditText)findViewById(R.id.et_reg_address);
        et_reg_comment=(EditText)findViewById(R.id.et_reg_commit);
        reg_return=(Button)findViewById(R.id.reg_return);


    }

    @Override
    void initEvents() {
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_reg_password.getText().toString().equals(et_reg_repassword.getText().toString())){
                    register();
                }
                else{
                    showToast("请确认两次密码相同");
                }

            }
        });
        reg_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent();
                intent.putExtra("user",et_reg_user.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
            }
        });


    }
    private void register(){
        userModel=new UserModel();
        userModel.reg(et_reg_user.getText().toString(),et_reg_password.getText().toString(),et_reg_telephone.getText().toString(),
                et_reg_adress.getText().toString(),et_reg_comment.getText().toString(),this);
    }


    @Override
    void initData() {

    }


    @Override
    public void onSuccess(ResultBean resultBean, int flag) {
        if(resultBean.getSuccess().equals("1")){
            intent=new Intent();
            intent.putExtra("user",et_reg_user.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
        }
        else{
            showToast("注册失败");
        }
    }

    @Override
    public void onFailure(String msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login_up);
    }
}
